package com.bop.dehong.home;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.NetWorkUtils;
import com.base.dehong.util.StringUtils;
import com.base.dehong.widget.banner.AutoScrollViewPager;
import com.base.dehong.widget.banner.CirclePageIndicator;
import com.base.dehong.widget.banner.ImagePagerAdapter;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseFragment;
import com.bop.dehong.base.DeHongApp;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.category.DeCaiFuListActivity;
import com.bop.dehong.config.Config;
import com.bop.dehong.decaifu.ToubiaoActivity;
import com.bop.dehong.decaifu.ToubiaoTyjActivity;
import com.bop.dehong.home.adapter.HomeListAdapter;
import com.bop.dehong.http.model.HomeModel;
import com.bop.dehong.info.entity.AdvEntity;
import com.bop.dehong.info.entity.HomeEntity;
import com.bop.dehong.info.entity.RecommendProject;
import com.bop.dehong.login.LoginActivity;
import com.bop.dehong.main.MainActivity;
import com.bop.dehong.webview.WebViewActivity;
import com.bop.dehong.widget.PullToRefreshView;
import com.bop.dehong.widget.PullToRefreshView.OnFooterRefreshListener;
import com.bop.dehong.widget.PullToRefreshView.OnHeaderRefreshListener;

@SuppressLint("NewApi")
public class HomeActivity extends BaseFragment implements OnHeaderRefreshListener, OnFooterRefreshListener {

	private AutoScrollViewPager adv_image;
	private CirclePageIndicator adv_circlePageIndicator;
	private ListView mListView;
	private PullToRefreshView mPullToRefreshView;
	private HomeModel homeModel;
	private HomeListAdapter mAdapter;
	private ImagePagerAdapter imgAdapter;
	private MainActivity mMainActivity;
	private List<AdvEntity> mAdvList;
	private List<String> mAdvStrList;
	private HomeEntity entity;
	private String bidid = "";
	private LinearLayout login_ll, logined_ll;
	private TextView available_funds_tv, make_reward_tv;

	public View onCreateView(android.view.LayoutInflater inflater, android.view.ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.activity_home, null);
		initTop(view);
		View headview = inflater.inflate(R.layout.headview_banner, null);
		login_ll = getView(headview, R.id.login_ll);
		logined_ll = getView(headview, R.id.logined_ll);
		available_funds_tv = getView(headview, R.id.available_funds_tv);
		make_reward_tv = getView(headview, R.id.make_reward_tv);
		registerViewClickListener(login_ll, logined_ll);
		adv_image = getView(headview, R.id.adv_image);
		adv_circlePageIndicator = getView(headview, R.id.adv_circlePageIndicator);
		mListView = getView(view, R.id.home_list);
		mListView.addHeaderView(headview);
		mPullToRefreshView = getView(view, R.id.home_pull_refresh_view);
		mAdvStrList = new ArrayList<String>();
		if (imgAdapter == null) {
			imgAdapter = new ImagePagerAdapter(
					HomeActivity.this, mAdvStrList, true);
		}
		adv_image.setAdapter(imgAdapter);
		adv_image.setInterval(5000);
		adv_image.startAutoScroll();
		adv_image.setCycle(true);
		adv_circlePageIndicator.setViewPager(adv_image);
		if(mAdapter == null){
			mAdapter = new HomeListAdapter(mMainActivity);
		}
		mListView.setAdapter(mAdapter);
		mListView.setOnItemClickListener(this);

		mPullToRefreshView.setOnHeaderRefreshListener(this);
		mPullToRefreshView.setOnFooterRefreshListener(this);
		return view;
		
	};
	
	RequestCallBack<HomeEntity> callback = new RequestCallBack<HomeEntity>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}
		
		public void onSuccess(HomeEntity info) {
			super.onSuccess(info);
			entity = info;
			mAdapter.appendToList(info.list);
			mAdvList = info.gallery;
			if (mAdvStrList.size() > 0) {
				mAdvStrList.clear();
			}
			for (int i = 0; i < mAdvList.size(); i++) {
				mAdvStrList.add(mAdvList.get(i).getImg());
			}
			imgAdapter.notifyDataSetChanged();
			bidid = entity.xsl.getBid();
		};
		
	};
	
	@Override
	public void initData() {
		if (NetWorkUtils.isConnect(mMainActivity)) {// 有网
			if (DeHongApp.mLoginEntity == null ){
				login_ll.setVisibility(View.VISIBLE);
				logined_ll.setVisibility(View.GONE);
			}else {
				login_ll.setVisibility(View.GONE);
				logined_ll.setVisibility(View.VISIBLE);
				String funds = DeHongApp.mLoginEntity.available_funds;
				if (StringUtils.isNotEmpty(funds))
				{
					available_funds_tv.setText(funds);
				}
				String reward = DeHongApp.mLoginEntity.make_reward;
				if (StringUtils.isNotEmpty(reward))
				{
					make_reward_tv.setText(reward);
				}
			}
			if (entity != null) {
				if (mAdapter.size() > 0) {
					mAdapter.clear();
				}
				mAdapter.appendToList(entity.list);
				mAdvList = entity.gallery;
				if (mAdvStrList.size() > 0) {
					mAdvStrList.clear();
				}
				for (int i = 0; i < mAdvList.size(); i++) {
					mAdvStrList.add(mAdvList.get(i).getImg());
				}
				imgAdapter.notifyDataSetChanged();
			}else
			{
				showProgressDialog();
				if (homeModel == null) {
					homeModel = new HomeModel(mMainActivity, callback);
				}
				homeModel.doNet();
			}
		}
	}

	@Override
	protected void initUI() {
		
	}

	
	@Override
	public void onHeaderRefresh(PullToRefreshView view) {
			mAdapter.clear();
			showProgressDialog();
			if (homeModel == null) {
				homeModel = new HomeModel(mMainActivity, callback);
			}
			homeModel.doNet();
			mPullToRefreshView.onHeaderRefreshComplete();
	}

	@Override
	public void onFooterRefresh(PullToRefreshView view) {
		mPullToRefreshView.onFooterRefreshComplete();
		openActivity(DeCaiFuListActivity.class);
	}

	protected void initTop(View parentView) {
		mPageName = getClass().getName();
		View mView = getView(parentView, R.id.home_top);
		TopManager manager = new TopManager(mView);
		manager.setTitle(R.string.home);
		manager.setLeftImageVisibility(View.GONE);
	}

	@Override
	protected int getContentViewId() {
		return R.layout.activity_home;
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (DeHongApp.mLoginEntity == null ){
			login_ll.setVisibility(View.VISIBLE);
			logined_ll.setVisibility(View.GONE);
		}else {
			login_ll.setVisibility(View.GONE);
			logined_ll.setVisibility(View.VISIBLE);
			String funds = DeHongApp.mLoginEntity.available_funds;
			if (StringUtils.isNotEmpty(funds))
			{
				available_funds_tv.setText(funds);
			}
			String reward = DeHongApp.mLoginEntity.make_reward;
			if (StringUtils.isNotEmpty(reward))
			{
				make_reward_tv.setText(reward);
			}
		}
		if (mMainActivity.isFristStart()) {
			initData();
			mMainActivity.setFristStart(false);
		}
		adv_image.startAutoScroll();
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		adv_image.stopAutoScroll();
	}
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mMainActivity = (MainActivity) activity;
	}

	@Override
	public void onClick(View v)
	{
		Intent intent = null;
		String url = "";
		String title = "";
	
		switch (v.getId()) {
		case ImagePagerAdapter.BannerClick:
			if (mAdvList != null && mAdvList.size() > 0) {
				int pos = (Integer) v.getTag();
				AdvEntity advEntity = mAdvList.get(pos);
				url = advEntity.getUrl();
				title = advEntity.getTitle();
			}
			break;
		case R.id.login_ll:
			Intent intent2 = new Intent(mMainActivity, LoginActivity.class);
			intent2.putExtra(Config.GO_BACK, true);
			startActivityForResult(intent2, 100);
			break;
		}
		
		if ( !TextUtils.isEmpty(url) && !TextUtils.isEmpty(title)) {
			intent = new Intent(mMainActivity, WebViewActivity.class);
			intent.putExtra("url", url);
			intent.putExtra("title", title);
			if (!TextUtils.isEmpty(bidid)){
				Config.GOBID = bidid;
			}
			intent.putExtra(Config.GO_BACK, true);
			startActivity(intent);
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		if (position - 1 >= 0)
		{
			mAdapter.setSelectPosition(position - 1);
			mAdapter.notifyDataSetChanged();
		}
		Bundle bundle = new Bundle();
		RecommendProject entity = mAdapter.get(position - 1);
		bundle.putString(Config.CAT_ID, entity.getId());
		bundle.putString(Config.CAT_NAME, entity.getTitle());
		int isemoney = 0;
		if (StringUtils.isNotEmpty(entity.getIsemoney()))
		{
			isemoney = Integer.valueOf(entity.getIsemoney());
		}	
		if (isemoney == 1) {
			openActivity(ToubiaoTyjActivity.class, bundle);
		}else {
			openActivity(ToubiaoActivity.class, bundle);
		}	
	}

	@Override
	protected void initTop()
	{
		// TODO Auto-generated method stub
		
	}	
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != Activity.RESULT_OK) {
			return;
		}
		if (requestCode == 100) {
			if (DeHongApp.mLoginEntity == null ){
				login_ll.setVisibility(View.VISIBLE);
				logined_ll.setVisibility(View.GONE);
			}else {
				login_ll.setVisibility(View.GONE);
				logined_ll.setVisibility(View.VISIBLE);
				String funds = DeHongApp.mLoginEntity.available_funds;
				if (StringUtils.isNotEmpty(funds))
				{
					available_funds_tv.setText(funds);
				}
				String reward = DeHongApp.mLoginEntity.make_reward;
				if (StringUtils.isNotEmpty(reward))
				{
					make_reward_tv.setText(reward);
				}
			}
		}
	}

}
